package kava.framework.util;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class GUIUtil {

	
	public static final String drawWrapText(Graphics g, String s, int bx, int by,
			int borderX0, int borderX1, Font font, boolean underLine,boolean focus,int vpad) {
		if (underLine) {
			Font f = Font.getFont(font.getFace(), font.getStyle()
					| Font.STYLE_UNDERLINED, font.getSize());
			g.setFont(f);
		}else{
			g.setFont(font);
		}
		int clipy = g.getClipY();
		int clipHeight = g.getClipHeight();
		
		int originalColor = g.getColor();
		int LH = g.getFont().getHeight() + vpad;
		int curIndex = 0;
		int count = s.length();
		while (curIndex < count) {
			char c = s.charAt(curIndex);
			if (c == '\r') {
				curIndex++;
				continue;
			}

			if (c == '\n') {
				by += LH;
				bx = borderX0;
				curIndex++;
				continue;
			}
			int cw = font.charWidth(c);
			if (bx + cw < borderX1) {
				if(focus){
					g.setColor(0x0000ff);
					g.fillRect(bx, by, cw, LH);
					g.setColor(0xffffff);
				}
				if( by > clipy + clipHeight || by + LH < clipy){
					//do nothing
				}else{
					g.drawChar(c, bx, by, Graphics.LEFT | Graphics.TOP);
				}
				
				bx += font.charWidth(c);
			} else {
				by += LH;
				bx = borderX0;
				if(focus){
					g.setColor(0x0000ff);
					g.fillRect(bx, by, cw, LH);
					g.setColor(0xffffff);
				}
				if( by > clipy + clipHeight || by + LH < clipy){
					//do nothing
				}else{
					g.drawChar(c, bx, by, Graphics.LEFT | Graphics.TOP);
				}
				bx += font.charWidth(c);
			}

			curIndex++;
		}
		g.setFont(font);
		g.setColor(originalColor);
		return bx + "," + by;
	}

	

	public static void drawHyperText(Graphics g, String text, int x, int y,
			int fgColor, boolean focus, int focusFgColor, int focusBgColor) {
		Font oldFont = g.getFont();
		int oldColor = g.getColor();
		g.setFont(Font.getFont(oldFont.getFace(),oldFont.getStyle() | Font.STYLE_UNDERLINED,oldFont.getSize()));
		if(!focus){
			g.setColor(fgColor);
			g.drawString(text, x, y, Graphics.LEFT|Graphics.TOP);
		}else{
			g.setColor(focusBgColor);
			g.fillRect(x, y, g.getFont().stringWidth(text), g.getFont().getHeight());
			g.setColor(focusFgColor);
			g.drawString(text, x, y, Graphics.LEFT|Graphics.TOP);
		}
		g.setColor(oldColor);
		g.setFont(oldFont);
	}

	/**
	 * 水平反转，即左右对称
	 * 
	 * @param g
	 *            Graphics 画笔
	 * @param img
	 *            Image 图片
	 * @param x
	 *            int 图片左上角的x坐标
	 * @param y
	 *            int 图片左上角的y坐标
	 */
	public static void drawConvertedImage(Graphics g, Image img, int x, int y,
			boolean isH) {
		if (img == null) {
			return;
		}
		// 获得图片大小
		int w = img.getWidth();
		int h = img.getHeight();
		if (isH) {
			// 绘制
			for (int i = 0; i < w; i++) {
				// 设置切割区域是1列
				g.setClip(x + i, y, 1, h);
				// 绘制图片
				g
						.drawImage(img, x - w + 2 * i, y, Graphics.LEFT
								| Graphics.TOP);
			}
		} else {
			// 绘制
			for (int i = 0; i < h; i++) {
				// 设置切割区域是1行
				g.setClip(x, y + i, w, 1);
				// 绘制图片
				g
						.drawImage(img, x, y - h + 2 * i, Graphics.LEFT
								| Graphics.TOP);
			}
		}

	}

	public static final String drawWrapText(Graphics g, String s, int bx,
			int by, int borderX0, int borderX1, Font font, boolean underLine,
			boolean focus) {
		return drawWrapText(g, s, bx, by, borderX0, borderX1, font, underLine,
				focus, 0);
	}

	public static void drawStrsCenter(String s, int cx, int y, Graphics g) {
		Font font = g.getFont();
		int lineH = font.getHeight();
		String[] sbs = Util.split(s, "\n");
		for (int i = 0; i < sbs.length; i++) {
			g.drawString(sbs[i], cx, y + i * lineH, Graphics.HCENTER
					| Graphics.TOP);
		}
	}

	public static void drawImageCenter(Graphics g, Image img, int x, int y,
			int w, int h) {
		if (img != null) {
			int iw = img.getWidth();
			int ih = img.getHeight();
			int tx = x + (w - iw) / 2;
			int ty = y + (h - ih) / 2;
			g.drawImage(img, tx, ty, Graphics.LEFT | Graphics.TOP);
		}
	}

	public static String getEllipsisString(String src, int len, Font font) {
		int sl = font.stringWidth(src);
		if (sl < len) {
			return src;
		} else {
			int endPos = src.length() - 1;
			while (true) {
				String s = src.substring(0, endPos) + "..";
				if (font.stringWidth(s) < len) {
					return s;
				} else {
					endPos--;
					if (endPos < 0) {
						return "";
					}
				}
			}
		}
	}
}
